import time
from dialog.PairDialog import PairDialog
from dialog.WorkoutDialog import WorkoutDialog
from data.runner import Runner
from data.partner import Partner
from data.workout import Workout
import string
# a very simple <strong class="highlight">stopwatch</strong> using Tkinter
# with Python30 replace Tkinter with tkinter

import Tkinter as tk
from stopwatchTimer import timer

# create a Tkinter window
root = tk.Tk()

tmr = timer()

def start(event=None):
    tmr.start()
    update_loop()

def update_loop():
    while True:
        if tmr.timing_flag == False:
            break
        # put the count value into the label
        root.label['text'] = "%.2f" % (tmr.get_time())
        # wait for 0.1 seconds
        time.sleep(1.0/70.0) #70ish fps
        # needed with time.sleep()
        root.update()
        
def stop(event=None):
    tmr.stop()
    
def split(event=None):
    t = tmr.split()
    if not t:
        return
    root.L.insert(tk.END, t)
    root.update()
    
def pop(event=None):
    if (len(tmr.splits)):
        val = tmr.splits.pop(0)
    else:
        val = tmr.get_time()
    root.L.delete(0, 0)
    name = event.widget
    try:
        #if this is the splits box
        name = name.name
        #change it to the name box
    except:
        #This is the name box
        pass
    name.split_vals.append(val)
    split_strings = []
    last = 0
    for split_time in name.split_vals:
        split_strings.append('%.2f'% (split_time-last))
        last = split_time
    name.splits.configure(text = ', '.join(split_strings))
    
def add(event=None):
    row_s = root.btn_add_partners.grid_info()['row']
    row_num = string.atoi(row_s) + len(root.runners) + 1
    d = PairDialog(root)
    if d.canceled:
        return
    first_runner = Runner(d.first)
    second_runner = Runner(d.second)
    print first_runner
    print second_runner
    partners = Partner(first_runner, second_runner)
    print partners
    
    name = tk.Label(root, text = '%s/%s' % (first_runner.name, second_runner.name) )
    name.grid(row=row_num, column = 0)
    name.split_vals = []
    name.bind("<Button-1>", pop)
    
    name.splits = tk.Label(root, bd=1)
    name.splits.grid(row=row_num, column = 1, columnspan=4, sticky = 'w')
    name.splits.name = name
    name.splits.bind("<Button-1>", pop)
    
    root.runners.append(name)
    
def open_workout(event=None):
    pass

def save_wo(event=None):
    d = WorkoutDialog(root)
    if d.canceled:
        return
    root.workout = Workout(d.name, d.date, d.description, id=None)
    

# create needed widgets
root.label = tk.Label(root, text='0.0', font=("Helvetica", 16))
root.btn_save = tk.Button(root, text='Save Workout...', command=save_wo)
#root.btn_split = tk.Button(root, text='Split', command=split)
#root.btn_stop = tk.Button(root, text='Stop', command=stop)
root.btn_add_partners = tk.Button(root, text='Add Partners...', command = add)
root.btn_open_workout = tk.Button(root, text='Open Workout...', command=open_workout)
root.runners = []

# use a grid to place the widgets
root.label.grid(row=0, column=0, columnspan=3)
root.btn_save.grid(row=1, column=0, padx=5, pady=5)
#root.btn_split.grid(row=1, column=1, padx=5)
#root.btn_stop.grid(row=1, column=2, padx=5)
root.btn_add_partners.grid(row=2, column=0)
root.btn_open_workout.grid(row=2, column=1)


root.s = tk.Scrollbar()
root.L = tk.Listbox()

'''root.s.pack(side=tk.RIGHT, fill=tk.Y)
root.L.pack(side=tk.LEFT, fill=tk.Y)'''
root.L.grid(row=0, column =3, rowspan=2)
root.s.grid(row=0, column =4, rowspan=2, sticky='ns')

root.s.config(command=root.L.yview)
root.L.config(yscrollcommand=root.s.set)

'''for i in range(30):
    root.L.insert(tk.END, str(i)*3)'''
    
# accept f12 key as the asynchronous split button
root.bind_all('<F12>', split)
root.bind_all('<F2>', start)
root.bind_all('<F3>', stop)

# start the event loop
root.mainloop()
